package com.mycjj.android.activity;

/**
 * 车圈 - 我的车圈 BY baiyuxiong @linewin.cc
 */
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.mycjj.android.ProjectDataManage;
import com.mycjj.android.R;
import com.mycjj.android.adapter.CarNetworkAdapter;
import com.mycjj.android.adapter.CarNetworkLastThemeAdapter;
import com.mycjj.android.bean.CarNetworkBaseBean;
import com.mycjj.android.bean.CarNetworkBean;
import com.mycjj.android.bean.CarNetworkLastThemesBaseBean;
import com.mycjj.android.bean.CarNetworkLastThemesBean;
import com.mycjj.android.http.ProjectHttpRequestImpl;
import com.mycjj.android.http.ProjectHttpRequestInterface;
import com.mycjj.android.util.Preferences;

public class CarNetworkListActivity extends BaseActivity {
	
	private ListView car_network_listview;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_car_network_list);
		init();
	}
	
	private void init(){
		initHeader();
		
		initData();
	}
	
	private ImageView mHeaderLeftImageview;
	private Button mHeaderRightButton;
	private TextView mHeaderTitleTextView;
	private void initHeader(){
		mHeaderLeftImageview = (ImageView) findViewById(R.id.header_left_button);
		mHeaderLeftImageview.setImageResource(R.drawable.back_selector);
		mHeaderRightButton = (Button) findViewById(R.id.header_right_button);
		mHeaderTitleTextView = (TextView) findViewById(R.id.header_title_textview);
		mHeaderTitleTextView.setText("我的车圈");
		mHeaderLeftImageview.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				CarNetworkListActivity.this.finish();
			}
		});
		mHeaderRightButton.setVisibility(View.GONE);
		
		car_network_listview = (ListView)findViewById(R.id.car_network_listview);
		
		car_network_listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent intent = new Intent(CarNetworkListActivity.this,
						CarNetworkDetailActivity.class);
				if(null != networkdataList)
				{
					CarNetworkBean bean = networkdataList.get(position);
					intent.putExtra("id", bean.getId());
				}
				startActivity(intent);
			}
		});
		
	}
	
	List<CarNetworkBean> networkdataList;
	private CarNetworkAdapter networkadapter;
	
	private void initData() {
		networkadapter = new CarNetworkAdapter(CarNetworkListActivity.this);

		//车圈
		ProjectHttpRequestInterface request = new ProjectHttpRequestImpl(
				CarNetworkListActivity.this, true);
		RequestParams params = new RequestParams();
		params.put("client_id", ProjectHttpRequestInterface.client_id_key);
		String storeid = Preferences.getSharedPreferences(CarNetworkListActivity.this,
				"storeId", "");
		params.put("store_id", storeid);
		params.put("token", Preferences.getSharedPreferences(CarNetworkListActivity.this,
				"user_token", ""));
		request.requestGetsJoinsWithDoneHandler("", params,
				new AsyncHttpResponseHandler() {
					@Override
					public void onSuccess(String content) {
						CarNetworkBaseBean activityBean = ProjectDataManage
								.getInstance().getCarNetworkBaseBean();
						Log.d("CarNetWorkFragment",
								(null == activityBean) ? "CarNetworkBaseBean is null"
										: "activityBean code"
												+ activityBean.getCode());
						if ((null != activityBean)
								&& (null != activityBean.getData())) {
							networkdataList = activityBean.getData();
							networkadapter.setList(networkdataList);
						}
					}

				});

		car_network_listview.setAdapter(networkadapter);
	}
}
